#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>


typedef int BTDataType;
typedef struct BinaryTreeNode
{
	BTDataType data;
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;

}BTNode;

BTNode* CreateBTNode()
{
	BTNode* n1 = (BTNode*)malloc(sizeof(BTNode));
	assert(n1);
	BTNode* n2 = (BTNode*)malloc(sizeof(BTNode));
	assert(n2);
	BTNode* n3 = (BTNode*)malloc(sizeof(BTNode));
	assert(n3);
	BTNode* n4 = (BTNode*)malloc(sizeof(BTNode));
	assert(n4);
	BTNode* n5 = (BTNode*)malloc(sizeof(BTNode));
	assert(n5);
	BTNode* n6 = (BTNode*)malloc(sizeof(BTNode));
	assert(n6);
	BTNode* n7 = (BTNode*)malloc(sizeof(BTNode));
	assert(n7);


	n1->data = 1;
	n1->left = n2;
	n1->right = n3;
	n2->data = 2;
	n2->left = n4;
	n2->right = NULL;
	n3->data = 3;
	n3->left = n5;
	n3->right = n6;
	n4->data = 4;
	n4->left = NULL;
	n4->right = NULL;
	n5->data = 5;
	n5->left = n7;
	n5->right = NULL;
	n6->data = 6;
	n6->left = NULL;
	n6->right = NULL;
	n7->data = 7;
	n7->left = NULL;
	n7->right = NULL;


	return n1;
}


int Binarynoleaf(BTNode* root)
{
	if (root == NULL)
		return 0;

	if (root->left == NULL && root->right == NULL)
		return 0;

	return 1 + Binarynoleaf(root->left) + Binarynoleaf(root->right);

}

int BinaryTree2(BTNode* root)
{
	if (root == NULL)
		return 0;

	if (root->left == NULL || root->right == NULL)
		return 0;

	return 1 + BinaryTree2(root->left) + BinaryTree2(root->right);

}

int Preorderk(BTNode* root,int k)
{
	if (root == NULL)
		return;

	static int i = 0;
	++i;
	if (k == i)
		printf("%d\n",root->data);

	Preorderk(root->left, k);
	Preorderk(root->right, k);

}

BTNode* BinaryFindpartent(BTNode* root, int p)
{
	if (root == NULL)
		return NULL;

	if ((root->left && root->left->data == p) || (root->right && root->right->data == p))
		return root;
		//printf("%d\n",root->data);

	BTNode* a = BinaryFindpartent(root->left, p);
	if (a != NULL)
		return a;
	BTNode* b = BinaryFindpartent(root->right, p);
		return b;


}


int main()
{
	BTNode* root = CreateBTNode();

	printf("%d\n", Binarynoleaf(root));

	printf("%d\n", BinaryTree2(root));

	Preorderk(root, 7);

	printf("%p\n",BinaryFindpartent(root, 7));

}